<?php
namespace Topxia\AdminBundle\Controller;

use Symfony\Component\HttpFoundation\Request;
use Topxia\Common\Paginator;
use Topxia\Common\ArrayToolkit;
use Topxia\Common\StringToolkit;
// use Topxia\Service\Order\OrderRefundProcessor\OrderRefundProcessorFactory;
//翟红利
class PromoController extends BaseController
{
	public function refundsAction(Request $request)
    {
      
        $conditions = $request ? $request->query->all() : array();
        $conditions['promo'] = isset($conditions['promo']) ? trim($conditions['promo']) : null;
        $conditions['mz'] = isset($conditions['mz']) ? trim($conditions['mz']) : null;
        $conditions['yxqtime'] = isset($conditions['yxqtime']) ? strtotime($conditions['yxqtime']) : null;
		$conditions['pc'] = isset($conditions['pc']) ? trim($conditions['pc']) : null;
        $count = $this->getPromoService()->getPromoCount(array_filter($conditions)); 
        $num=$this->getPromoService()->getMyPromo($count);
		
        $paginator = new Paginator($this->get('request'), $count, 20);
		
        $activemas = $this->getPromoService()->getPromo(
            array_filter($conditions),
            $paginator->getOffsetCount(),
            $paginator->getPerPageCount()
        );
        if($num==""){
            $num="1";
        }else{
            $num=$num[0]['pc']+1;
        }
        
        $time=date('Y-m-d');
        return $this->render('TopxiaAdminBundle:Promo:refunds.html.twig', array(
            'num'       =>$num,
            'paginator' => $paginator,
            'activemas' => $activemas,
            'time'      => $time
        ));
    }

   public function buildAction(Request $request)
    {
        // $conditions = $request->query->all();
        $a=date("Y-m-d H:i:s");
        $a_time = strtotime($a);
        $conditions=array();
        $conditions['num']=$request->get('num');
        $conditions['pc']=$request->get('pc');
        $conditions['mz']=$request->get('mz');
        $conditions['yxq']=strtotime('+' .$request->get('yxq').'Month',$a_time);

        $success = $this->getPromoService()->buildding($conditions);

        if($success == '1'){
            $this->setFlashMessage('success', '生成成功！');
        }else{
            $this->setFlashMessage('danger', '生成失败');
        }
        return $this->redirect($this->generateUrl('admin_Promo_refunds'));
    }
    public function setAction(Request $request)
    {

        $conditions = $request->query->all();
        if(isset($conditions['set']) && $conditions['set'] == '1')
            $success = $this->getActiveMaService()->set($conditions);
        elseif(isset($conditions['reset']) && $conditions['reset'] == '1')
            $success = $this->getActiveMaService()->reset($conditions);

        $this->setFlashMessage('success', '设置成功！');
        return $this->redirect($this->generateUrl('admin_activema_show'));
    }
   protected function getPromoService()
    {
        return $this->getServiceKernel()->createService('Kuke.PromoService');
    }

}